﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSBTC.Model;
using MSBTC.ReportViewer;

namespace MSBTC.WinForms
{
    public partial class MatMaintenance : Form

    {
        MDIParent mdipar = new MDIParent();
        PMSContext dbcontext = new PMSContext();
        FormHelpers help = new FormHelpers();
        Mat_Maintenance dbpj = new Mat_Maintenance();
        private string Datamanip;
        private int rowselect;
    
        public MatMaintenance()
        {
            InitializeComponent();
        }

        

        private void MatMaintenance_Load(object sender, EventArgs e)
        {
            RefreshFields();
            RefreshDB();

            

            txtMatname.TabIndex = 1;
            txtDesc.TabIndex = 2;
            btnClear.TabIndex = 3;
            btnRefresh.TabIndex = 4;
            btnClose.TabIndex = 5;
        }

        #region Functions
        private void RefreshDB()
        {
            //retrieve all data of projects in List
            var prj = (from c in dbcontext.Mat_Maintenances
                       select c).ToList();

            LVMatM.Items.Clear();
            if (prj.Count != 0)
            {
                foreach (var sqlPrj in prj)
                {
                    //add items in listview box
                    ListViewItem lvitems = LVMatM.Items.Add(sqlPrj.DataID.ToString());
                    lvitems.SubItems.Add(sqlPrj.Material_Name.ToString());
                    lvitems.SubItems.Add(sqlPrj.Description.ToString());


                }
            }
        }

        private void ClearFields()
        {
            txtID.Clear();
            txtMatname.Clear();
            txtDesc.Clear();
            

        }

        private void DisableFields()
        {
            txtID.Enabled = false;
            txtMatname.Enabled = false;
            
            txtDesc.Enabled = false;
            
        }

        private void EnableFields()
        {
            txtID.Enabled = true;
            txtMatname.Enabled = true;
          
            txtDesc.Enabled = true;
            
        }

        private void RefreshFields()
        {
            DisableFields();
            ClearFields();

            LVMatM.Enabled = true;
            btnNew.Enabled = true;
            btnDel.Enabled = false;
            btnEdit.Enabled = false;
            btnClear.Enabled = true;
            btnSave.Enabled = false;    
        }
        #endregion

        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearFields();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshFields();
            LVMatM.Enabled = true;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            Datamanip = "Add";

            EnableFields();
            ClearFields();
            txtMatname.Focus();
            
            btnSave.Enabled = true;
            btnNew.Enabled = false;
            btnEdit.Enabled = false;
            btnDel.Enabled = false;
            LVMatM.Enabled = false;
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Datamanip = "Edit";

            EnableFields();
            txtMatname.Focus();
            btnSave.Enabled = true;
            btnNew.Enabled = false;
            btnDel.Enabled = false;
            btnEdit.Enabled = false;
           
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if ((txtMatname.Text == "") || (txtDesc.Text == ""))
            {
                MessageBox.Show("Please fill up the missing fields", "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            
            else
            {

                if (Datamanip == "Add")
                {

                    var add = (from p in dbcontext.Mat_Maintenances
                               where p.Material_Name == txtMatname.Text
                               select p).SingleOrDefault();

                    if (add == null)
                    {

                        dbpj.Material_Name = txtMatname.Text;
                        dbpj.Description = txtDesc.Text;
                            

                        dbcontext.Entry(dbpj).State = EntityState.Added;
                        dbcontext.SaveChanges();


                        MessageBox.Show("Successfully Saved", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        RefreshFields();
                        RefreshDB();
                        return;
                    }

                    else
                    {
                        DialogResult YN = MessageBox.Show("Item already exist, do you want to replace it?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        if (YN == DialogResult.Yes)
                        {
                            var replace = dbcontext.Mat_Maintenances.Find(add.DataID);

                            replace.Material_Name = txtMatname.Text;
                            replace.Description = txtDesc.Text;

                            dbcontext.Entry(replace).State = EntityState.Modified;
                            dbcontext.SaveChanges();

                            MessageBox.Show("Successfully Modified", "Modify", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            RefreshFields();
                            RefreshDB();
                            return;

                        }
                    }
                
                    LVMatM.Enabled = true;
                }

                else if (Datamanip == "Edit")
                {

                    /*TODO check for new item listed in the 2nd listbox that does not exist in the database.. and save
                    to database
                         
                    */
                    DialogResult YN = MessageBox.Show("Do you want to replace it?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (YN == DialogResult.Yes)
                    {
                        #region editted items

                        var replace = dbcontext.Mat_Maintenances.Find(Convert.ToInt32(txtID.Text));

                        replace.Material_Name = txtMatname.Text;
                        replace.Description = txtDesc.Text;
                       

                        dbcontext.Entry(replace).State = EntityState.Modified;
                        dbcontext.SaveChanges();

                        MessageBox.Show("Successfully Modified", "Modify", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        RefreshFields();
                        RefreshDB();

                        #endregion
                    }
                }
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            DialogResult YN = MessageBox.Show("Do you want to Delete it?", "Deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (YN == DialogResult.Yes)
            {
                var del = dbcontext.Mat_Maintenances.Find(Convert.ToInt32(txtID.Text));
                if (del != null)
                {

                    dbcontext.Entry(del).State = EntityState.Deleted;
                    dbcontext.SaveChanges();
                }
                RefreshFields();
                RefreshDB();
            }
        }

        

        private void LVMatM_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            //focus the selected items
            rowselect = LVMatM.FocusedItem.Index;

            //getting the information from the listview to textbox
            txtID.Text = LVMatM.Items[rowselect].SubItems[0].Text;
            txtMatname.Text = LVMatM.Items[rowselect].SubItems[1].Text;
            txtDesc.Text = LVMatM.Items[rowselect].SubItems[2].Text;

            btnDel.Enabled = true;
            btnEdit.Enabled = true;
            btnNew.Enabled = true;
            btnSave.Enabled = false;


        }



    }
}
